<?php
namespace Admin\Controller;
use Common\Model\FollowModel;

use Think\Controller;
use Admin\Controller\AdminController;

use Think\Verify;

class FuturesCommissionController extends AdminController{

	/*页面加载*/
	public function index(){
		$followTable   =   D('follow');
		$commissionTable = D('Futures_commission');
		/*分配状态查询*/
		if($_REQUEST['status']){
			if($_REQUEST['status']==1){
				$sql['a.status']=array('eq',1);
				$status=1;
			}elseif($_REQUEST['status']==2){
				$sql['a.status']=array('eq',0);
				$status=2;
			}else{
				$status=-1;
			}
		}
		else{
			$status=-1;
		}
		$this->assign('status',$status);
		/*下单者查询*/
		if($_REQUEST['Name']){
			$map['nickname']=array('like',"%".trim($_REQUEST['Name'])."%");
			$user = M('Follow')->where($map)->field('id')->select();
			foreach($user as $v){
				$uid = $uid.$v['id'].',';
			}
			$uid = trim($uid,',');
			$sql['a.order_user_id'] = array('in',$uid);
		}
		/*分佣者查询*/
		if($_REQUEST['Cname']){
			$map['nickname']=array('like',"%".trim($_REQUEST['Cname'])."%");
			$user = M('Follow')->where($map)->field('id')->select();
			foreach($user as $v){
				$uid = $uid.$v['id'].',';
			}
			$uid = trim($uid,',');
			$sql['a.commission_user_id'] = array('in',$uid);
		}
		/*时间查询*/
		$create_time=I('get.create_time');
		if($create_time){
			$sql['a.create_time']=array('egt',strtotime($create_time));
			$this->assign('create_time',$create_time);
		}
		$end_time=I('get.end_time');
		if($end_time){
			$end_time=strtotime($end_time)+86400;
			$sql['_string']='a.create_time <='.$end_time;
			$this->assign('end_time',$end_time);
		}
		$mobile = I('get.mobile');
		if($mobile){
			$sql['b.mobile'] = array('like',"%".trim($mobile)."%");
		}
		//分页
		$count	=	$commissionTable->alias('a')->join("wp_follow b on a.commission_user_id = b.id")->where($sql)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

		/*查询佣金数据*/
		$list = $commissionTable->alias('a')->join("wp_follow b on a.commission_user_id = b.id")
		->where($sql)
		->limit($Page->firstRow.','.$Page->listRows)
		->order('a.id desc')->field('a.id,a.order_user_id,b.nickname,a.commission,a.status,a.note,a.update_time,a.create_time,b.mobile')
		->select();
		//佣金分配
		$money_data=$commissionTable->alias('a')->join("wp_follow b on a.commission_user_id = b.id")->field('a.*,sum(a.commission) as topmoney')->where($sql)->group('a.status')->select();
		for($i = 0;$i < count($money_data);$i++){
			$money_data[$i]['topmoney'] = round($money_data[$i]['topmoney'],2);
		}
		$this->assign('money_data',$money_data);
		$this->assign('_page',$show);
		$this->assign('list',$list);
		$this->display();
	}

	/*佣金分配*/
	public function status(){
		$user = array();	//用户数组
		$money = array();	//佣金数组
		$time = time();		//获取时间
		/*
		 * 搜索条件
		 * */
		/*分配状态查询*/
		if($_POST['status']){
			if($_POST['status']==1){
				$sql['status']=array('eq',1);
				$status=1;
			}elseif($_POST['status']==2){
				$sql['status']=array('eq',0);
				$status=2;
			}else{
				$status=-1;
			}
		}
		else{
			$status=-1;
		}
		$this->assign('status',$status);
		/*下单者查询*/
		if($_POST['Name']){
			$map['nickname']=array('like',"%".trim($_POST['Name'])."%");
			$user_data = M('Follow')->where($map)->field('id')->select();
			foreach($user_data as $v){
				$uid = $uid.$v['id'].',';
			}
			$uid = trim($uid,',');
			$sql['order_user_id'] = array('in',$uid);
		}
		/*分佣者查询*/
		if($_POST['Cname']){
			$map['nickname']=array('like',"%".trim($_POST['Cname'])."%");
			$user_data = M('Follow')->where($map)->field('id')->select();
			foreach($user_data as $v){
				$uid = $uid.$v['id'].',';
			}
			$uid = trim($uid,',');
			$sql['commission_user_id'] = array('in',$uid);
		}
		/*时间查询*/
		if ($_POST['create_time']) {
			$time=strtotime($_POST['create_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['create_time'] = array('between',"$start_time,$end_time");
		}

		/*
		 * 选中或全部分配
		 */
		$set = array('status' => '1','update_time' => $time);		//需更改的数据
		if(!empty($_POST['Allsubmit'])){//一键分配
			/*
			if(empty($sql)){
				$sql['status'] = 0;
			}
			*/
			$sql['status'] = 0;
			$list = M('futures_commission')->where($sql)->select();			//查询佣金未被分配的订单
			M('futures_commission')->where($sql)->save($set);		//更改状态
			foreach ($list as $v){
				$id = $v['commission_user_id'];		//获取用户ID
				/*计算佣金*/
				if(in_array($v['commission_user_id'],$user)){
					$money[$id] = $money[$id]+$v['commission'];
				}else{
					$user[$id] = $v['commission_user_id'];
					$money[$id] = $v['commission'];
				}
			}
			/*分配佣金并记录*/
			foreach ($money as $k => $v){
				$openid = M('Follow')->where('id='.$k)->getField('Openid');
				modify_account($openid,$v,'6','分配佣金');
			}
		}else{//选择分配
			foreach($_POST['ids'] as $v){

				$lwhere['id'] = $v;
				$lwhere['status'] = 0;
				$list = M('futures_commission')->where($lwhere)->find();			//根据选中ID查询订单
				$id = $list['commission_user_id'];		//获取用户ID
				/*计算佣金*/
				if($list['status'] == 0){
					M('futures_commission')->where('id='.$v)->save($set);		//更改状态
					if(in_array($list['commission_user_id'],$user)){
						$money[$id] = $money[$id]+$list['commission'];
					}else{
						$user[$id] = $list['commission_user_id'];
						$money[$id] = $list['commission'];
					}
				}
			}
			/*分配佣金并记录*/
			foreach ($money as $k => $v){
				$openid = M('Follow')->where('id='.$k)->getField('Openid');
				modify_account($openid,$v,'6','分配佣金');
			}
		}
		$this->success('分配成功');
	}

	/*
	*	佣金统计页面
	*/
	public function count(){
		//分页
		$data	=	M("futures_commission")->alias('a')->join("wp_follow b on a.commission_user_id = b.id")->group('a.commission_user_id')->select();// 查询满足要求的总记录数
		$count = count($data);
		$Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出

		$list=M("futures_commission")->alias('a')->join("wp_follow b on a.commission_user_id = b.id")->limit($Page->firstRow.','.$Page->listRows)->field('b.id,b.nickname,b.mobile')->group('a.commission_user_id')->select();
		foreach($list as $k=>$v){
			$al_commission = M("futures_commission")->where("status = 1 and commission_user_id = ".$v['id'])->getField("sum(commission) as money");
			$not_commission = M("futures_commission")->where("status = 0 and commission_user_id = ".$v['id'])->getField("sum(commission) as money");
			$list[$k]['al_money'] = empty($al_commission) ? 0.00 : round($al_commission,2);
			$list[$k]['not_money'] = empty($not_commission) ? 0.00 : round($not_commission,2);
		}
		$this->assign('_page',$show);
		$this->assign('list',$list);
		$this->display();
	}
}
